package com.woniu.microprojectserver.bean;


import com.woniu.microcommonsserver.entity.BuildInfoEntity;
import com.woniu.microcommonsserver.entity.SpaceInfoEntity;

import java.util.ArrayList;
import java.util.List;

public class BuildTreeUtils {

    public List<TreeData> getTree(TreeData treeData,List<BuildInfoEntity> lists) {

        List<TreeData> treeList = new ArrayList<>();
        for(int i = 0; i < lists.size(); i++){
            if(treeData.getId().equals( lists.get(i).getHigherId())){
                TreeData td = new TreeData();
                td.setId(lists.get(i).getId());
                td.setLabel(lists.get(i).getName());
                td.setRank(lists.get(i).getRank());
                td.setHigherId(lists.get(i).getHigherId());
                td.setChildren(new BuildTreeUtils().getTree(td,lists));
                treeList.add(td);
            }
        }
        return treeList;
    }

    public List<TreeData> getAllTree(TreeData treeData,List<BuildInfoEntity> buildList,List<SpaceInfoEntity> spaceList) {
        List<TreeData> treeList = new ArrayList<>();
        for(int i = 0; i < buildList.size(); i++){
            if(treeData.getId().equals( buildList.get(i).getHigherId())){
                TreeData td = new TreeData();
                td.setId(buildList.get(i).getId());
                td.setLabel(buildList.get(i).getName());
                td.setSpace(false);
                td.setHigherId(buildList.get(i).getHigherId());
                List<TreeData> children = getAllTree(td,buildList,spaceList);
                td.setChildren(children);
                treeList.add(td);
            }else if(i == buildList.size()-1 && !treeData.isR1()){
                for (int x = 0; x < spaceList.size(); x++) {
                    SpaceInfoEntity space = spaceList.get(x);
                    if(space.getFkBuildId().equals(treeData.getId())){
                        TreeData td2 = new TreeData();
                        td2.setId(space.getId());
                        td2.setLabel(space.getAbbreviation());
                        td2.setFkBuildId(space.getFkBuildId());
                        td2.setHigherId(space.getHigherId());
                        td2.setSpace(true);
                        td2.setFloor(space.getFloor());
                        td2.setChildren(new BuildTreeUtils().getSpaceTree(td2.getId(),spaceList));
                        treeList.add(td2);
                    }
                }
            }
        }
        return treeList;
    }

    public List<TreeData> getSpaceTree(Long id,List<SpaceInfoEntity> lists) {

        List<TreeData> treeList = new ArrayList<>();
        for(int i = 0; i < lists.size(); i++){
            if(id.equals( lists.get(i).getHigherId())){
                TreeData td = new TreeData();
                td.setId(lists.get(i).getId());
                td.setLabel(lists.get(i).getAbbreviation());
                td.setFkBuildId(lists.get(i).getFkBuildId());
                td.setHigherId(lists.get(i).getHigherId());
                td.setSpace(true);
                td.setFloor(lists.get(i).getFloor());
                td.setChildren(new BuildTreeUtils().getSpaceTree(td.getId(),lists));
                treeList.add(td);
            }
        }
        return treeList;
    }


}